package com.mysoft.gfyx.clue.service.impl;

import com.mysoft.framework.common.exception.BusinessLogicException;
import com.mysoft.framework.common.util.MappingUtil;
import com.mysoft.framework.modeling.dto.OptionItem;
import com.mysoft.framework.service.AppService;
import com.mysoft.gfyx.clue.controller.dto.Perm.SaveProfitProvinceRequestDto;
import com.mysoft.gfyx.clue.remoteservice.RemoteAreaService;
import com.mysoft.gfyx.clue.remoteservice.RemoteProfitService;
import com.mysoft.gfyx.clue.remoteservice.dto.AreaDto;
import com.mysoft.gfyx.clue.remoteservice.dto.GetProfitProvinceDataDto;
import com.mysoft.gfyx.clue.remoteservice.dto.ProvinceAndCityConditionDto;
import com.mysoft.gfyx.clue.service.*;
import com.mysoft.gfyx.clue.service.dto.*;
import com.mysoft.gfyx.common.time.TimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 描述：ProfitProvince 接口层
 *
 * @author JavaSdk generator
 * @date 2023-03-16
 */
@Service
public class ProfitProvinceServiceImp extends AppService implements ProfitProvinceService {
    @Autowired
    private RemoteProfitService remoteProfitService;

    @Autowired
    private TimeService timeService;

    @Autowired
    private RemoteAreaService remoteAreaService;

    public GetProfitProvinceDto getProfitProvince() {
        GetProfitProvinceDataDto getProfitProvinceDataDto = remoteProfitService.getProfitProvince();
        GetProfitProvinceDto res = MappingUtil.getSimpleMapperFacade().map(getProfitProvinceDataDto, GetProfitProvinceDto.class);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date thirtyDay = new Date();
            if (res.getLastSettingTime() != null) {
                Date lastSettingTime = sdf.parse(res.getLastSettingTime());
                thirtyDay = timeService.getDateAfter(30, lastSettingTime);
                res.setNextSettingTime(sdf.format(thirtyDay));
            }

            if (getProfitProvinceDataDto.getProvinceNumType().equals(RemoteProfitService.ProvinceNumType.Province.toString()) && getProfitProvinceDataDto.getProvinceNum() > 0) {
                if (res.getLastSettingTime() == null) {
                    res.setIsEnableSubmit(1);
                } else {
                    if ((new Date()).getTime() <= thirtyDay.getTime()) {//当前时间小于30天后的时间，不可以编辑
                        res.setIsEnableSubmit(0);
                    } else {
                        res.setIsEnableSubmit(1);
                    }
                }

            } else {
                res.setIsEnableSubmit(0);
            }
            //文案提示
            if (getProfitProvinceDataDto.getProvinceNumType().equals(RemoteProfitService.ProvinceNumType.Province.toString())) {
                res.setTip("已开通" + res.getProvinceNum() + "个省份的线索雷达权限，还有" + res.getAvailableNum() + "个未设置省份");
            } else {
                res.setTip("已开通全国的线索雷达权限，无需设置省份权限");
            }
            return res;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 获取有权益的省份（不含城市）
     *
     * @return
     */
    public List<OptionItem> getProfitProvinceList() {
        GetProfitProvinceDataDto getProfitProvinceDataDto = remoteProfitService.getProfitProvince();
        List<OptionItem> result = new ArrayList<>();
        ProvinceAndCityConditionDto conditionDto = new ProvinceAndCityConditionDto();
        //conditionDto.setIsIncludeNationwide(true);
        List<AreaDto> res = remoteAreaService.getProvince(conditionDto);
        if (getProfitProvinceDataDto.getProvinceNumType().equals(RemoteProfitService.ProvinceNumType.Province.toString())) {
            result = getProfitProvinceDataDto.getProvinceList();
        } else {
            List<OptionItem> finalResult = result;
            res.forEach(areaDto -> {
                OptionItem item = new OptionItem();
                item.setValue(areaDto.getItemCode());
                item.setParent(areaDto.getParentCode());
                item.setText(areaDto.getItemName());
                finalResult.add(item);
            });
        }
        return result;
    }

    /**
     * 获取有权益的省份、城市
     */
    public List<OptionItem> getProfitProvinceAndCity(List<String> provinceCodeList) {
        ProvinceAndCityConditionDto conditionDto = new ProvinceAndCityConditionDto();
        conditionDto.setProvinceCode(provinceCodeList);
        List<AreaDto> res = remoteAreaService.getProvinceAndCity(conditionDto);

        List<OptionItem> result = new ArrayList<>();
        res.forEach(areaDto -> {
            OptionItem item = new OptionItem();
            item.setValue(areaDto.getItemCode());
            item.setParent(areaDto.getParentCode());
            item.setText(areaDto.getItemName());
            result.add(item);
        });
        return result;
    }

    public void saveProfitProvince(SaveProfitProvinceRequestDto dto) {
        GetProfitProvinceDataDto getProfitProvinceDataDto = remoteProfitService.getProfitProvince();
        if (Objects.equals(getProfitProvinceDataDto.getProvinceNumType(), "NationWide")) {
            throw new BusinessLogicException("已开通全国的线索雷达权限，无需设置省份权限");
        }

        if (dto.getProvinceList().size() > getProfitProvinceDataDto.getProvinceNum()) {
            throw new BusinessLogicException("仅限开通" + getProfitProvinceDataDto.getProvinceNum() + "个省份的权限，请重新操作");
        }
        remoteProfitService.saveProfitProvince(dto);
    }


    public List<OptionItem> provinceOption() {
        List<OptionItem> items = new ArrayList<>();
        ProvinceAndCityConditionDto conditionDto = new ProvinceAndCityConditionDto();
        List<AreaDto> res = remoteAreaService.getProvince(conditionDto);

        res.forEach(areaDto -> {

            OptionItem item = new OptionItem();
            item.setValue(areaDto.getItemCode());
            item.setParent(areaDto.getParentCode());
            item.setText(areaDto.getItemName());

            items.add(item);
        });

        return items;
    }
}
